package com.wl.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wl.entity.Dept;
import com.wl.service.DeptService;
import com.wl.util.CommonMessage;
import com.wl.util.PageData;

@Controller
public class DeptController {
	
	@Autowired
	private DeptService ds;
	
	/**
	 * 查询所有
	 * @return
	 */
	@GetMapping(value = "seldeptall")
	@ResponseBody
	public List<Dept> seldeptall() {
		List<Dept> list = ds.selectAllDept();
		return list;
		
	}
	
	/**
	 * 查询方法 根据参数返回请求数据，ajax请求
	 * @param page  页码
	 * @param limit 每页显示的记录数
	 * @param content 模糊查询的关键字
	 * @return
	 */
	@RequestMapping(value = "dept", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Dept> listAdmin(Integer page, Integer limit, String content) {
		if (content == null) {
			content = "";
		}
		PageData<Dept> pd = ds.selectDeptByLike(page, limit, content);
		// 返回 json格式的对象
		pd.setCode(0);
		pd.setMsg("请求成功");
		return pd;
	}
	
	/**
	 * 根据部门名称查询部门信息
	 * @param deptname
	 * @return
	 */
	@GetMapping(value = "seldeptbyname/{deptname}", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Dept selectByName(@PathVariable("deptname") String deptname) {
		Dept dept=ds.selectDeptByName(deptname);
		return dept;
	}
	
	/**
	 * 删除
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value = "deldept/{deptid}", method = RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage delDept(@PathVariable(value = "deptid") Integer deptid) {
		boolean flag = ds.delDeptById(deptid);
		CommonMessage cm = new CommonMessage("删除成功");
		if (!flag) {
			cm.setMsg("删除失败");
		}
		return cm;
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping(value = "updept", method = RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateDept(@RequestBody Dept dept) {
		boolean flag = ds.updateDept(dept);
		CommonMessage cm = new CommonMessage("修改成功");
		if (!flag) {
			cm.setMsg("修改失败");
		}
		return cm;
	}
	
	/**
	 * 增加
	 * @param admin
	 * @return
	 */
	@PostMapping(value = "adddept")
	@ResponseBody
	public CommonMessage addDept(@RequestBody Dept dept) {
		boolean flag = ds.addDept(dept);
		CommonMessage cm = new CommonMessage("添加成功");
		if (!flag) {
			cm.setMsg("添加失败");
		}
		return cm;
	}
	
	
	/**
	 * 按照部门名称查找部门审批人
	 * @param deptname
	 * @return
	 */
	@GetMapping(value = "selectapprove")
	@ResponseBody
	public Dept selectByDeptName(String deptname) {
		Dept pd = ds.selectByDeptName(deptname);
		return pd;
	}
	
	
}
